<?php
namespace Android\Model;

use Think\Model;

class JpushMapping2Model extends Model {

    /**
     * 更新教师->极光映射
     *
     * @param $teacher
     * @param $client_id
     */
    public function refresh($teacher,$client_id) {

        if (empty($teacher) || empty($client_id))
            return false;

        $data['tid'] = $teacher['id'];
        $data['teacher_id'] = $teacher['teacher_id'];
        $mapping = $this->where($data)->find();
        if (!empty($mapping)) {
            return $this->where($data)->setField('client_id',$client_id);
        }

        $data['client_id'] = $client_id;
        return $this->add($data);
    }


    /**
     * 获取极光接口所需要的receiver
     *
     * @param $idArr
     * @return string
     */
    public function getReceiver($idArr) {

        if (empty($idArr)) return array('registration_id' => array());

        $registration_id = array();
        foreach ($idArr as $key => $val) {

            $client_id = $this->where('tid = '.intval($val))->getField('client_id');
            if (empty($client_id)) continue;
            $registration_id[] = $client_id;
        }

        return array('registration_id'=>$registration_id);
    }


    /**
     * 根据学生id，获取单个Receiver
     *
     * @param $sid
     * @return array|bool
     */
    public function getReceiverByTid($tid) {
        if (empty($tid)) return false;

        $client_id = $this->where('tid = '.$tid)->getField('client_id');
        if (empty($client_id)) return false;

        return array('registration_id'=>array($client_id));
    }

}